package org.foreveross.que.applicationImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.foreveross.que.entity.SayEntity;
import org.foreveross.que.entity.TopicEntity;
import org.foreveross.que.infra.SayRepository;
import org.foreveross.que.infra.TopicRepository;
import org.foreveross.que.infra.base.constant.SysConstants;
import org.openkoala.koala.auth.ss3adapter.AuthUserUtil;
import org.openkoala.koala.auth.ss3adapter.CustomUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.foreveross.que.application.SayApplication;
import org.foreveross.que.vo.IndexVo;
import org.foreveross.que.vo.PageData;
import org.foreveross.que.vo.SayVo;
import org.foreveross.que.vo.TopicVo;
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
@Service
public class SayApplicationImpl implements SayApplication {

	@Autowired SayRepository sayRepository ;
	@Autowired TopicRepository	topicRepository ;
	

@SuppressWarnings("null")
@Transactional
@Override
public SayEntity reply(IndexVo vo) throws Exception {
	String tid = vo.getTid();
	TopicEntity topic = topicRepository.get(TopicEntity.class, tid);
	SayVo svo = vo.getSayVo();
	SayEntity entity = new SayEntity();
	Date currDate = new Date();
	CustomUserDetails user = vo.getLoginUser();
	String username = null;
	if(user!=null){
		username = user.getUsername();
	}
	entity.setCreateTime(currDate);
	entity.setLastTime(currDate);
	entity.setReplyRole(vo.getRoleid());//该参数从loadAuthority方法装载过来
	entity.setSayInfo(svo.getSayInfo());
	entity.setState(SysConstants.ONE_INT);
	entity.setSysType(topic.getSysType());
	entity.setTid(vo.getTid());
	entity.setUaccount(username);
	entity.setUname(username);
	this.sayRepository.create(entity);
	Integer replyCount = topic.getReplyCount()==null?0:topic.getReplyCount();
	topic.setLastTime(currDate);
	topic.setReplyCount(replyCount+1);
	return entity;
}
@Transactional
@Override
public void answer(IndexVo vo) throws Exception {
	SayEntity sayEntity = this.sayRepository.find(SayEntity.class, vo.getSayVo().getId());
	TopicEntity topic = this.topicRepository.get(TopicEntity.class, vo.getTid());
	topic.setReplyId(sayEntity.getId());
	topic.setReplyAccount(sayEntity.getUaccount());
	topic.setReplyInfo(sayEntity.getSayInfo());
	topic.setReplyName(sayEntity.getUname());
	topic.setReplyRole(sayEntity.getReplyRole());
	topic.setLastTime(new Date());
	this.topicRepository.update(topic);
}

@Transactional
@Override
public void loadReplys(IndexVo vo) throws Exception {
	PageData pageData = vo.getPageData();
	Map params = this.loadParams(vo);
	List<SayEntity> entitys = this.sayRepository.findSays(params,pageData.getPageIndex(), pageData.getPageLen());
	Long dataSize = sayRepository.findSaysCount(params);
	pageData.setResult(getTopicVos(entitys));
	pageData.setDataSize(dataSize);
	TopicEntity topic = this.topicRepository.get(TopicEntity.class, vo.getTid());
	if(!vo.getWithOutTopic()){//点击进入话题是，浏览数+1
	Integer browseCount = topic.getBrowseCount()==null?0:topic.getBrowseCount();
	topic.setBrowseCount(browseCount+1);
	}
	
}

public void deleteReply(IndexVo vo) throws Exception{
	SayEntity entity = this.sayRepository.find(SayEntity.class, vo.getSayVo().getId());
	if(StringUtils.equals(entity.getUaccount(), vo.getLoginUser().getUsername())){
		
		this.sayRepository.delete(SayEntity.class, vo.getSayVo().getId());
		vo.setMsg("删除成功");
	}else{
		vo.setMsg("无权限删除");
	}
	
}

private Map loadParams(IndexVo vo) throws Exception {
	Map map = new HashMap();
	map.put("tid",vo.getTid());
	map.put("sysType",vo.getSysType());
	return map;
}

private List getTopicVos (List<SayEntity> entitys) throws Exception{
	List says = new ArrayList();
	for(SayEntity entity:entitys){
		SayVo vo = new SayVo();
		vo.load(entity);
		says.add(vo);
	}
	return says;
}
	
	
}
